package com.school.campus.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.school.common.annotation.Log;
import com.school.common.core.controller.BaseController;
import com.school.common.core.domain.AjaxResult;
import com.school.common.enums.BusinessType;
import com.school.campus.domain.Club;
import com.school.campus.service.IClubService;
import com.school.common.utils.poi.ExcelUtil;
import com.school.common.core.page.TableDataInfo;

/**
 * 社团管理Controller
 *
 * @author wwj
 * @date 2025-11-05
 */
@RestController
@RequestMapping("/campus/club")
public class ClubController extends BaseController {
    @Autowired
    private IClubService clubService;

    /**
     * 查询社团管理列表
     */
    @PreAuthorize("@ss.hasPermi('campus:club:list')")
    @GetMapping("/list")
    public TableDataInfo list(Club club) {
        startPage();
        List<Club> list = clubService.selectClubList(club);
        return getDataTable(list);
    }

    /**
     * 导出社团管理列表
     */
    @PreAuthorize("@ss.hasPermi('campus:club:export')")
    @Log(title = "社团管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Club club) {
        List<Club> list = clubService.selectClubList(club);
        ExcelUtil<Club> util = new ExcelUtil<Club>(Club.class);
        util.exportExcel(response, list, "社团管理数据");
    }

    /**
     * 获取社团管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('campus:club:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(clubService.selectClubById(id));
    }

    /**
     * 新增社团管理
     */
    @PreAuthorize("@ss.hasPermi('campus:club:add')")
    @Log(title = "社团管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Club club) {
        return toAjax(clubService.insertClub(club));
    }

    /**
     * 修改社团管理
     */
    @PreAuthorize("@ss.hasPermi('campus:club:edit')")
    @Log(title = "社团管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Club club) {
        return toAjax(clubService.updateClub(club));
    }

    /**
     * 删除社团管理
     */
    @PreAuthorize("@ss.hasPermi('campus:club:remove')")
    @Log(title = "社团管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clubService.deleteClubByIds(ids));
    }

    /**
     * 关注社团
     */
    @PreAuthorize("@ss.hasPermi('campus:club:followClub')")
    @Log(title = "社团管理", businessType = BusinessType.DELETE)
    @PutMapping("/follow/{id}")
    public AjaxResult followClub(@PathVariable Long id){
        Club club = clubService.selectClubById(id);
        return toAjax(clubService.updateClub(club));
    }
}
